/**
 * 返回成功结果
 * @param data 数据
 * @param message 消息
 */
export const resultSuccess = (data: any, message = 'ok') => {
  return {
    code: 200,
    data,
    message,
    success: true
  }
}

/**
 * 返回错误结果
 * @param message 错误消息
 * @param code 错误码
 */
export const resultError = (message = 'Request failed', code = 500) => {
  return {
    code,
    data: null,
    message,
    success: false
  }
}

/**
 * 从请求中获取 token
 * @param request 请求对象
 */
export const getRequestToken = (request: any) => {
  return request.headers?.authorization?.replace('Bearer ', '')
}

/**
 * 生成随机 ID
 */
export const randomId = () => {
  return Math.random().toString(36).slice(2)
}

/**
 * 生成随机时间
 * @param start 开始时间
 * @param end 结束时间
 */
export const randomTime = (start?: Date, end?: Date) => {
  const startTime = start || new Date(2024, 0, 1)
  const endTime = end || new Date()
  const randomTimestamp = startTime.getTime() + Math.random() * (endTime.getTime() - startTime.getTime())
  return new Date(randomTimestamp).toISOString().replace('T', ' ').substring(0, 19)
}

/**
 * 分页处理
 * @param list 列表数据
 * @param page 页码
 * @param pageSize 每页条数
 */
export const pagination = <T>(list: T[], page = 1, pageSize = 10) => {
  // 确保 page 和 pageSize 是数字类型
  const pageNum = Number(page)
  const pageSizeNum = Number(pageSize)
  
  const startIndex = (pageNum - 1) * pageSizeNum
  const endIndex = startIndex + pageSizeNum
  return {
    list: list.slice(startIndex, endIndex),
    total: list.length
  }
} 